package com.example.tao.mycar.login;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.avos.avoscloud.SignUpCallback;
import com.example.tao.mycar.MyActionBarActivity;
import com.example.tao.mycar.MyApplication;
import com.example.tao.mycar.R;
import com.example.tao.mycar.activity.HomeActivity;
import com.example.tao.mycar.utils.RequestUtils;

import java.util.Calendar;

public class SetBabyBirthdayActivity extends MyActionBarActivity implements View.OnClickListener {

    private Button nextButton;
    private Button cancelButton;
    private Button confirmButton;
    private TextView birthdayText;
    private LinearLayout datepickerLayout;
    private DatePickerDialog datePickerDialog;
    private Calendar calendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_set_baby_birthday);
        setToolBar(getResources().getString(R.string.label_baby_birthday), true);

        birthdayText = (TextView)this.findViewById(R.id.text_birthday);
        nextButton = (Button)this.findViewById(R.id.next_step);
        cancelButton = (Button)this.findViewById(R.id.cancel);
        confirmButton = (Button)this.findViewById(R.id.confirm);
        datepickerLayout = (LinearLayout)this.findViewById(R.id.datepicker_layout);

        birthdayText.setOnClickListener(this);
        nextButton.setOnClickListener(this);
        cancelButton.setOnClickListener(this);
        confirmButton.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.text_birthday :
                if (datePickerDialog != null && datePickerDialog.isShowing()) {
                    return;
                }
                //datepickerLayout.setVisibility(View.VISIBLE);
                calendar = Calendar.getInstance();
                datePickerDialog = new DatePickerDialog(SetBabyBirthdayActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        birthdayText.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

                datePickerDialog.show();
                break;
            case R.id.cancel :
                birthdayText.setText("");
                //datepickerLayout.setVisibility(View.GONE);
                break;
            case R.id.confirm :
                //datepickerLayout.setVisibility(View.GONE);
                break;
            case R.id.next_step :
                RequestUtils.getSharedPreferenceEditor(this).putString(RequestUtils.BABYBIRTHDAY, birthdayText.getText().toString()).commit();
                registerUserAccount();
                break;
            default:
                break;
        }
    }

    private void registerUserAccount() {
        AVUser user = new AVUser();
        String userName, password, phoneNum;
        userName = RequestUtils.getSharedPreference(this).getString(RequestUtils.USERNAME, null);
        password = MyApplication.getPassword();
        phoneNum = userName;
        if (userName != null && password != null) {
            user.setUsername(userName);
            user.setPassword(password);
            user.setMobilePhoneNumber(phoneNum);
            user.signUpInBackground(new SignUpCallback() {
                @Override
                public void done(AVException e) {
                    if (e == null) {
                        setBabyInfo();
                    } else {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    private void setBabyInfo() {
        AVObject post = new AVObject("BabyInfo");
        post.put(RequestUtils.BABYSEX, RequestUtils.getSharedPreference(this).getString(RequestUtils.BABYSEX, null));
        post.put(RequestUtils.BABYBIRTHDAY, RequestUtils.getSharedPreference(this).getString(RequestUtils.BABYBIRTHDAY, null));
        post.put("post", AVUser.getCurrentUser());
        post.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    Toast.makeText(SetBabyBirthdayActivity.this, "信息保存成功！", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(SetBabyBirthdayActivity.this, HomeActivity.class);
                    startActivity(intent);
                } else {
                    e.printStackTrace();
                }
            }
        });
    }
}
